//
//  YWWebViewController.m
//  网易新闻
//
//  Created by Yeewii@hotmail.com on 16/6/28.
//  Copyright © 2016年 Yeewii@hotmail.com. All rights reserved.
//

#import "YWWebViewController.h"
#import "YWHttpTool.h"
#import <WXApiObject.h>
#import <WXApi.h>

@interface YWWebViewController ()
@property (nonatomic,strong) UIWebView *webView;
@property (nonatomic,copy) NSDictionary *resDic;

@end

@implementation YWWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"reader_share"] style:UIBarButtonItemStylePlain target:self action:@selector(share)];
    [self requestData];
    

}

-(void)requestData
{
    __weak typeof(self) weakSelf = self;
    [YWHttpTool GET:[NSString stringWithFormat:@"http://c.3g.163.com/nc/article/%@/full.html",_url] params:nil success:^(NSDictionary *responseObject) {
        NSLog(@"%@",responseObject);
        dispatch_async(dispatch_get_main_queue(), ^{
           
            _resDic = [responseObject copy];
            [weakSelf.webView loadHTMLString:responseObject[_url][@"body"] baseURL:nil];
            
            
        });
    } failure:^(NSError *error) {
        
    }];
}

-(UIWebView *)webView
{
    if (_webView == nil)
    {
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, MAINWIDTH, MAINHEIGHT)];
        [self.view addSubview:_webView];
    }
    return _webView;
}
-(void)dealloc
{
    self.webView = nil;
}

-(void)share
{
    
    SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
    sendReq.bText = NO;//不使用文本信息
    sendReq.scene = 0;//0 = 好友列表 1 = 朋友圈 2 = 收藏
    
    //创建分享内容对象
    WXMediaMessage *urlMessage = [WXMediaMessage message];
    urlMessage.title = _resDic[_url][@"dkeys"]&&[_resDic[_url][@"dkeys"]length]>0?_resDic[_url][@"dkeys"]:@"SB";
    urlMessage.description = _resDic[_url][@"title"];
    if ([_resDic[_url][@"img"]count]>0)
    {
        [urlMessage setThumbImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_resDic[_url][@"img"][0][@"src"]] options:NSDataReadingMappedIfSafe error:nil]]];//分享图片,使用SDK的setThumbImage方法可压缩图片大小
    }
    //创建多媒体对象
    WXWebpageObject *webObj = [WXWebpageObject object];
    webObj.webpageUrl = _resDic[_url][@"shareLink"];//分享链接
    
    //完成发送对象实例
    urlMessage.mediaObject = webObj;
    sendReq.message = urlMessage;
    
    //发送分享信息
    [WXApi sendReq:sendReq];
}
@end
